package com.example.fangwei.utilsapp.retrofit;

import android.content.Context;
import com.example.fangwei.utilsapp.BaseEntity;
import com.example.fangwei.utilsapp.MyApp;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 作者：${yinlin-fangwei}
 */

public abstract class RetrofitResultCallBack<T extends BaseEntity> implements Callback<T>{

    private Context mContext = MyApp.getAppContext();
    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        int code1 = response.code();
        if(200!=code1){
//            ToastUtils.showToast(mContext,code1 + mContext.getResources().getString(R.string.netErrText));
//            onFailure(new Throwable(mContext.getResources().getString(R.string.codeError)+code1+"---"+mContext.getResources().getString(R.string.netErrText)));
            return;
        }
        String code = response.body().getRetCode()!= null ?response.body().getRetCode():"01" ;
//        String message = response.body().getRetMsg();
//        LogUtil.e("onResponse:  == " + response.body().toString() );
        switch(code){
            case "0000"://请求成功
                onSuccess(response.body());
                break;
            case "100101"://输入参数错误
            case "9999"://系统异常
            case "8888"://系统繁忙,请稍候再试!
            case "6666"://请求参数为空
            case "default"://系统异常
            case "100102"://客户端异常
            case "100103"://通讯超时
            case "100104"://通讯异常
            case "200005"://同一手机号，一分钟之内只能获取一次验证码
            case "000000013"://客户端已是最高版本
//                onFailure(new Throwable(mContext.getResources().getString(R.string.codeError)+code));
//                ToastUtils.showToast(mContext,message);
                break;
            case  "7777":
//                onFailure(new Throwable(mContext.getResources().getString(R.string.loginAgain)));
//                ToastUtils.showToast(mContext,mContext.getResources().getString(R.string.pleaseLoginAgain));
//                SharedPerferenceUtil.saveData(MyApp.getAppContext(),"isLogin",false);
//                SharedPerferenceUtil.saveData(MyApp.getAppContext(),"userToken","");
                break;
            default:
//                ToastUtils.showToast(mContext,message);
//                onFailure(new Throwable(mContext.getResources().getString(R.string.codeError)+code));
                break;
        }



    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {
        onFailure(t);
    }

    public abstract void onSuccess(T t);
    public abstract void onFailure(Throwable throwable);

}
